<template>
	<view class="main" style="background-color: #e4ecf7;min-height: 100vh;">

		<view class="loan_box">
			<view class="loan_title_box">
				<view class="title" >
          <uni-icons :type="iconType" size="15" style="color: #ff0000;" @tap="changeMange">
						<span style="font-size: 15px;">{{text}}</span>
						</uni-icons>
				</view>
				<view class="link" @tap="gotoAdd">
				    <uni-icons type="plusempty" size="15" style="color: #ff0000;">
						<span style="font-size: 15px;">新增地址</span>
						</uni-icons>
				</view>
			</view>
			<view class="loan_list">
				<view class="list_item" v-for="(item,index) in productList" :key="index" @tap="editAddress(item.addressId)">
					<view class="item_head">
						<view class="loan_info">
							<view class="name" style="font-size: 20rpx;">
								{{item.addressCity}}
							</view>
							<view class="date" style="font-size: 30rpx;color: black;">
								{{item.addressArea}}
							</view>
						</view>
						<view class="join_btn" @tap="gotoEdit(item.addressId)">
					<uni-icons type="compose" size="30"></uni-icons>
						</view>
					</view>
					<view class="item_bottom">
						<view class="bottom_item">
							<view class="names" style="color: black;">
								{{item.addressHost}}  
							</view>
							<!-- 点击管理显示 -->
							<view class="amount" v-show="!isShow">
                          <label>
        <checkbox v-model="item.addressStatus" :disabled="true" :checked="item.addressStatus === true" activeBackgroundColor="#fe7300"/>
        默认
                         </label>
							</view>
						</view>
						<view class="bottom_item">
							<view class="names" style="color: black;">
								{{item.addressHostTel}}
							</view>
							<view class="amount" v-show="!isShow">
							     <img src="/static/blank.png" alt="" />
							</view>
						</view>
						<view class="bottom_item">
							<view class="names" >
    <button v-if="item.addressType === '家'" type="primary" size="mini">{{item.addressType}}</button>
    <button v-else-if="item.addressType === '公司'" type="default" size="mini" style="background-color: #55aa00;color: white;">{{item.addressType}}</button>
    <button v-else-if="item.addressType === '学校'" type="default" size="mini" style="background-color: #ff007f;color: white;">{{item.addressType}}</button> 
    <button v-else type="default" size="mini">{{item.addressType}}</button>
							</view>
							<!-- 点击管理显示 -->
					         <view class="amount" v-show="!isShow">
						     <button type="warn" size="mini" @tap="deleteItem(item.addressId,index)">删除</button>
						    </view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="foot_line" >
			<view class="line"></view>
			<text style="margin: 0 20rpx;font-size: 28rpx;">我的也有底线的</text>
			<view class="line"></view>
		</view>
	
	</view>
</template>

<script>
	import { store } from '@/uni_modules/uni-id-pages/common/store.js'
		import { get, post, removeToken } from '@/utils/api.js';
	export default {
		data() {
			return {
				productList:[],
				      // 初始状态
				      isManaging: true,
				      // 数据绑定
				      text: '管理',
					  iconType:'compose',
					  isShow:true,
			}
		},
		onLoad() {

		},
		created() {
				post('/address/getAddressList', {
				  addressUser: store.userInfo.username,
				}, {
				  needToken: true, 
				  success: (res) => {
					if (res.statusCode === 200) {
						this.productList = res.data.data
					  console.log('获取用户收货地址成功');
					}
				  }
				});
		},
		methods: {
			editAddress(addressId) {
				if(uni.getStorageSync('selectAddress'))
				{
					uni.setStorageSync('addressId',addressId)
					uni.redirectTo({
						url:'/pages/proDetail/proDetail'
					})
				}

				uni.removeStorageSync('selectAddress')
			},
			    deleteItem(addressId,index) {
					// 使用 splice 方法从数组中移除指定索引的元素
					 this.productList.splice(index, 1);
					post('/address/deleteAddressInfo', {
					  addressId: addressId,
					}, {
					  needToken: true, 
					  success: (res) => {
						if (res.statusCode === 200) {
						  console.log('删除收货地址成功');
						  uni.showToast({ title: '删除成功！', icon: 'success', duration: 2000});
						}
					  }
					});
			    },
             gotoAdd(){
				 uni.navigateTo({
				 	url:'/pages/addAddress/addAddress'
				 })
			 },
			 gotoEdit(addressId){
				 uni.setStorageSync('addressId', addressId);
			 	 uni.navigateTo({
			 		url:'/pages/editAddress/editAddress'
				 })
			 },
			// 切换管理，图标及其对应显示内容的方法
		     changeMange() {
			       // 利用Vue的响应式数据改变状态
			    this.isManaging = !this.isManaging;
			    this.text = this.isManaging ? '管理' : '退出管理';
                 this.iconType = this.isManaging ? 'compose' : 'redo'; 
				this.isShow = !this.isShow
		    },
		}
	}
</script>

<style lang="scss" scoped>

	.loan_box{
		background-color: #fff;
		width: 90%;
		margin: 20rpx auto;
		border-radius: 10rpx;
		padding: 10rpx 20rpx;
		padding-bottom: 20rpx;
		box-sizing: border-box;
		.loan_title_box{
			display: flex;
			align-items: center;
			padding-top: 6rpx;
			.title{
				font-size: 30rpx;
			}
			.link{
				margin-left: auto;
				color: #797979;
				font-size: 20rpx;
			}
		}
		.loan_list{
			margin-top: 30rpx;
			.list_item{
				padding: 0 10rpx;
				border-bottom: 1px solid #e9e9e9;
				margin-top: 20rpx;
				.item_head{
					display: flex;
					align-items: center;
					.pic{
						height: 100rpx;
						width: 100rpx;
						border-radius: 50%
					}
					.loan_info{
						margin-left: 20rpx;
						.name{
							color: #797979;
							font-weight: 600;
						}
						.date{
							color: #797979;
							margin-top: 6rpx;
						}
					}
					.join_btn{
						background-color: #ffffff;
						color: #fff;
						margin-left: auto;
						margin-right: 20rpx;
						padding: 6rpx 30rpx;
						border-radius: 30rpx;
						font-size: 28rpx;
					}
				}
				.item_bottom{
					display: flex;
					margin-top: 20rpx;
					padding-bottom: 14rpx;
					width: 100%;
					.bottom_item{
						flex: 1;
						display: flex;
						flex-direction: column;
						align-items: flex-start;
						justify-content: center;
						font-size: 26rpx;
						.names{
							color: #797979;
						}
						.amount{
							color: #ff0548;
							margin-top: 6rpx;
							font-weight: 600;
						}
					}
				}
			}
		}
	}
	.foot_line{
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 20rpx;
		color: #5f5f5f;
		.line{
			width: 200rpx;
			border-bottom: 1px solid #5f5f5f;
		}
	}
</style>